package com.example.partyplaner;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class SingleEventActivity extends Activity  implements AsyncResponse {

	TextView titleTextView;
	TextView locationTextView;
	TextView dataTextView;
	TextView descriptionTextView;
	TextView organizerTextView;
	private String responseMsg;
	private int responseCode;
	Button tasksButton;
	int partyId;
	String value;
	int logout=0;
	DatabaseConnection db;
	String filename="SingleEvent";
	FileOutputStream outputStream;
	FileInputStream inputStream;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_single_event);
		partyId = getIntent().getIntExtra("partyId", 999);
		titleTextView = (TextView) findViewById(R.id.textView1);
		locationTextView = (TextView) findViewById(R.id.textView2);
		dataTextView = (TextView) findViewById(R.id.textView3);
		organizerTextView = (TextView) findViewById(R.id.textView4);
		descriptionTextView = (TextView) findViewById(R.id.textView5);
		
	
		tasksButton = (Button) findViewById(R.id.button1);
		
		Bundle extras = getIntent().getExtras();
		if(MainActivity.noInternetMode==0){
			if (extras != null) {
			    value = extras.getString("EVENT_ID");
			    this.db = new DatabaseConnection(SingleEventActivity.this);
			    this.db.delegate = SingleEventActivity.this;
			    this.db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/ShowEvent/"+MainActivity.token, "GET", value);
				
			}
		}
		else{
			try {
				FileInputStream fis;
				fis = openFileInput(filename);
				ObjectInputStream is = new ObjectInputStream(fis);
				JSONObject jObject = (JSONObject) is.readObject();
				is.close();
				titleTextView.setText(jObject.getString("name"));
				locationTextView.setText("Where: "+jObject.getString("place"));
				dataTextView.setText("When: "+jObject.getString("date"));
				organizerTextView.setText("Organizer: "+jObject.getString("ownerName")+" "+jObject.getString("ownerSurname"));
				descriptionTextView.setText(jObject.getString("description"));	
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (StreamCorruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		tasksButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),TasksActivity.class);
				intent.putExtra("EVENT_ID", value);		
				startActivity(intent);
			}
		});

		titleTextView.setText("title and id " + partyId);
		locationTextView.setText("Where ?");
		dataTextView.setText("When ?");
		descriptionTextView
				.setText("long description will be here, so read it");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.single_event, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.logout) {
			this.db = new DatabaseConnection(SingleEventActivity.this);
			this.db.delegate = SingleEventActivity.this;
			db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/Logout", "GET", MainActivity.token);
			logout=1;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	public void processFinish(WebserviceResponse output) {
		if(output!=null){
			responseMsg=output.getResponseMsg();
			responseCode=output.getResponseCode();
			
			if (responseCode==200){
				if (logout!=1)
				{
					try {				
						JSONArray jArray = new JSONArray(responseMsg);
						JSONObject jObject= jArray.getJSONObject(0);						
						titleTextView.setText(jObject.getString("name"));
						locationTextView.setText("Where: "+jObject.getString("place"));
						dataTextView.setText("When: "+jObject.getString("date"));
						organizerTextView.setText("Organizer: "+jObject.getString("ownerName")+" "+jObject.getString("ownerSurname"));
						descriptionTextView.setText(jObject.getString("description"));	
						try {
							outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
							ObjectOutputStream os;
							os = new ObjectOutputStream(outputStream);
							os.writeObject(jObject);
						} catch (IOException e) {
							e.printStackTrace();
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
					
				}
				else{
					Intent intent = new Intent(getApplicationContext(),MainActivity.class);
					startActivity(intent);
				}
			}
			else{
				Log.i("PartyPlanner", "Fail");
			}	
		}
		else{
			Log.i("PartyPlanner", "NULL");
		}
	}
	
	public Dialog onCreateDialog(int dialogId) {
		switch (dialogId) {
			case MainActivity.PLEASE_WAIT_DIALOG:
				ProgressDialog dialog = new ProgressDialog(SingleEventActivity.this);
				dialog.setTitle("Connecting");
				dialog.setMessage("Please wait....");
				dialog.setCancelable(true);
				return dialog;
			default:
				break;
		}
		return null;
	}
}
